package com.hqd.sellrelation.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hqd.common.model.PageParams;
import com.hqd.common.mybatis.base.service.impl.BaseServiceImpl;
import com.hqd.common.utils.StringUtils;
import com.hqd.sellrelation.server.dto.VisitDto;
import com.hqd.sellrelation.server.dto.VisitSearchDto;
import com.hqd.sellrelation.server.entity.VisitBusinessInfo;
import com.hqd.sellrelation.server.entity.VisitInfo;
import com.hqd.sellrelation.server.entity.VisitPersonInfo;
import com.hqd.sellrelation.server.enums.VisitEnum;
import com.hqd.sellrelation.server.mapper.VisitBusinessInfoMapper;
import com.hqd.sellrelation.server.mapper.VisitInfoMapper;
import com.hqd.sellrelation.server.mapper.VisitPersonInfoMapper;
import com.hqd.sellrelation.server.service.VisitInfoService;
import com.hqd.sellrelation.server.vo.VisitInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class VisitInfoServiceImpl extends BaseServiceImpl<VisitInfoMapper, VisitInfo> implements VisitInfoService {

    @Autowired
    private VisitInfoMapper visitInfoMapper;
    @Autowired
    private VisitBusinessInfoMapper businessInfoMapper;
    @Autowired
    private VisitPersonInfoMapper personInfoMapper;
    /**
     * 分页查询
     *
     * @param pageParams
     * @return
     */
    @Override
    public IPage<VisitInfoVo> findListPage(PageParams pageParams, VisitSearchDto dto) {
        return visitInfoMapper.selectByPage(pageParams,dto);
    }

    @Override
    @Transactional
    public boolean updateOrSave(VisitDto dto) {
        //人员
        if (VisitEnum.type.PERSON.getValue() == dto.getType()) {
            VisitPersonInfo personInfo = new VisitPersonInfo();
            BeanUtils.copyProperties(dto, personInfo);
            personInfo.setAddress(dto.getAddressPerson());
            personInfo.setReckon(dto.getReckonPerson());
            if(StringUtils.isEmpty(dto.getId())){
                personInfoMapper.insert(personInfo);
            }else {
                personInfoMapper.updateByVisitId(personInfo);
            }
        } else {
            //企业
            VisitBusinessInfo businessInfo = new VisitBusinessInfo();
            BeanUtils.copyProperties(dto, businessInfo);
            if(StringUtils.isEmpty(dto.getId())){
                //新增
                businessInfoMapper.insert(businessInfo);
            }
            businessInfoMapper.updateByVisitId(businessInfo);
        }
        VisitInfo visitInfo = new VisitInfo();
        BeanUtils.copyProperties(dto, visitInfo);
        if(StringUtils.isEmpty(dto.getId())){
            //新增
            visitInfoMapper.insert(visitInfo);
        }else{
            visitInfoMapper.updateById(visitInfo);
        }
        return true;
    }

    @Override
    @Transactional
    public void cancelVisit(String id) {
        VisitInfo visitInfo = visitInfoMapper.selectById(id);
        if(visitInfo==null){
            return;
        }
        visitInfo.setVisitType((short) VisitEnum.visitType.CANCEL.getValue());//取消
        visitInfoMapper.updateById(visitInfo);
    }

}
